import { Component, OnInit } from '@angular/core';

declare var saveAs: any;
declare var form: any;
declare var base: any;

@Component({
  templateUrl: './build.java.html'
})
export class BuildJavaComponent implements OnInit {
  fs: any = null;
  bfs: any = null;

  type = 1;
  proName: any = 'itfreer-xx';
  packageName: any = 'com.itfreer.xx';

  constructor() {
    this.fs = new form.FormTreeService('metadatadirectory', {
      orderObject: { order: 0 },
      keyField: 'id',
      randomFields: ['id'],
      pageSize: 1000,
      defaultQueryObject: { 'directoryType:in': '1,2' }
    });
    // 加载表单数据
    this.fs.loadData();

    // 构建表单
    this.bfs = new form.FormTreeService('generate', {
      keyField: 'id'
    });
  }

  ngOnInit(): void {
  }

  start(mr, type) {
    this.type = type;
    this.bfs.addRow();
    this.bfs.er.mateID = mr.id;
    this.bfs.er.classAliseName = mr.display;
    this.bfs.er.tableName = mr.id;

    const ss = mr.id.split('_');
    let className = '';
    ss.forEach(s => {
      className += s.replace(s[0], s[0].toUpperCase());
    });
    this.bfs.er.className = className;
    this.bfs.er.dicName = className.toLowerCase();

    this.bfs.er.exeidFieldName = 'exeid';
    this.bfs.er.bpmStateFieldName = 'bpmState';

    this.bfs.er.tenant = true;
    this.bfs.er.tenantIDFieldName = 'tenantID';
    this.bfs.er.tenantNameFieldName = 'tenantName';

    this.bfs.er.logicalDelete = false;
    this.bfs.er.logicalDeleteFieldName = 'logicalDelete';

    this.bfs.er.tree = false;
    this.bfs.er.treePIDFieldName = 'pid';

    this.bfs.er.create = false;
    this.bfs.er.createUserIDFieldName = 'createUserid';
    this.bfs.er.createUserNameFieldName = 'createUsername';
    this.bfs.er.createTimeFieldName = 'createTime';

    this.bfs.er.update = false;
    this.bfs.er.updateUserIDFieldName = 'updateUserid';
    this.bfs.er.updateUserNameFieldName = 'updateUsername';
    this.bfs.er.updateTimeFieldName = 'updateTime';
  }

  build() {
    let url = base.BaseService.getHostUrl() + '/generate/java/';
    if (this.type === 1) {
      url += 'simple';
    } else if (this.type === 2) {
      url += 'dictionary';
    } else if (this.type === 3) {
      url += 'bpm';
    } else if (this.type === 4) {
      url += 'print';
    }
    this.bfs.isSaveing = true;
    this.bfs.er.proName = this.proName;
    this.bfs.er.packageName = this.packageName;
    base.BaseService.postSubscribe(url, this.bfs.er, (res: any) => {
      saveAs(res.body, this.bfs.er.classAliseName + '.zip');
      this.bfs.isSaveing = false;
      this.bfs.closeEdit();
    });
  }
}
